package com.lzq.springblog.controller;

import com.lzq.springblog.common.Result;
import com.lzq.springblog.common.ResultCode;
import com.lzq.springblog.dao.UserInfoMapper;
import com.lzq.springblog.model.BlogInfo;
import com.lzq.springblog.model.UserInfo;
import com.lzq.springblog.service.UserService;
import com.lzq.springblog.utils.JwtUtils;
import com.lzq.springblog.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@Tag(name = "用户接口")
@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Operation(description = "用户登录")
    @PostMapping("/login")
    public Result login(String username,String password) {
         if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
             log.error("参数不合法: username:{}, password:{}",username,password);
             return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
         }
        UserInfo user = userService.getUserInfoByName(username);
        if (user==null || !SecurityUtils.verify(password,user.getPassword())) {
            return Result.failed(ResultCode.FAILED_LOGIN);
        }
        //登录成功,返回token
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",user.getId());
        claims.put("username",user.getUserName());
        String token = JwtUtils.genJwt(claims);
        log.info("生成token:{}"+token);
        return Result.success(token);
    }

    @Operation(description = "用户注册")
    @PostMapping("/registry")
    public Result registry(String username,String password,String repeatPassword,String githubUrl) {
        log.info("开始执行registry方法");
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)
                || !StringUtils.hasLength(repeatPassword)) {
            log.error("参数不合法: username:{}, password:{}",username,password);
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        if(!password.equals(repeatPassword)) {
            return Result.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME);
        }
        UserInfo user = userService.getUserInfoByName(username);
        if (user!=null) {
            return Result.failed(ResultCode.FAILED_USER_EXISTS);
        }
        String finalPassword = SecurityUtils.encrypt(password);
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(username);
        userInfo.setPassword(finalPassword);
        if (StringUtils.hasLength(githubUrl)) {
            log.info("用户设置了github地址");
            userInfo.setGithubUrl(githubUrl);
        }
        Integer result = userService.addUser(userInfo);
        if(result<1) {
            log.error("新增用户失败");
        }
        return Result.success(true);
    }

    @Operation(description = "获取登录用户信息")
    @GetMapping("/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        String jwt = request.getHeader("user_token");
        log.info("开始执行getUserInfo方法");
        Integer userId = (Integer) JwtUtils.getUserIdFromToken(jwt).getData();
        if (userId!=null) {
            UserInfo userInfo = userService.getUserInfoById(userId);
            log.info("查询到用户信息:{}",userInfo);
            return Result.success(userInfo);
        }
        log.error("查询的用户信息不存在");
        return Result.failed(ResultCode.FAILED);
    }

    @Operation(description = "获取作者信息")
    @GetMapping("/getAuthorInfo")
    public Result getAuthorInfo(Integer blogId) {
        log.info("开始执行getAuthorInfo方法,blogId:{}",blogId);
        if (blogId==null || blogId<1) {
            return Result.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        UserInfo userInfo = userService.getUserInfoByBlogId(blogId);
        log.info("查询到用户信息:{}",userInfo);
        if (userInfo==null) {
            log.error("查询的用户信息不存在");
            return Result.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        return Result.success(userInfo);
    }




}
